package com.nblog.controller.admin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.nblog.annotation.ControllerBind;
import com.nblog.auth.annotation.RequiresPermissions;
import com.nblog.constant.ApiConstant;
import com.nblog.interceptor.admin.LoginInterceptor;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.model.SinglePage;
import com.nblog.resp.ApiResp;
import com.nblog.utils.CommonUtils;
import com.nblog.view.InvokeResult;

@ControllerBind(controllerKey="/admin/page")
@Before({LoginInterceptor.class,SysLogInterceptor.class})
public class PageController extends BaseControlller {
	@RequiresPermissions(value={"/admin/page"})
	public void index(){
		render("index.html");
	}
	@RequiresPermissions(value={"/admin/page"})
	public void getData(){
		String title=getPara("startTime");
		StringBuffer sql=new StringBuffer("from single_page where 1=1");
		if(StrKit.notNull(title)){
			sql.append("and title='"+title+"' ");
		}
		sql.append(this.getOrderbyStr());
		Page<SinglePage> page=SinglePage.dao.paginate(getParaToInt("page", 1), getParaToInt("rows",20), "select * ",sql.toString());
		System.out.println(page.getTotalRow());
		renderJson(CommonUtils.pageToDataGrid(page));
	}
	@RequiresPermissions(value={"/admin/page"})
	public void edit(){
		render("eidt.html");
	}
	@RequiresPermissions(value={"/admin/page"})
	public void savePage(){
		Integer id=getParaToInt("id");
		ApiResp resp=new ApiResp();
		if(id==null){
			SinglePage page=new SinglePage();
			page.setTitle(getPara("title")).setKeywords(getPara("keywords"));
			page.setDescription(getPara("description")).setContent(getPara("content"));
			page.setCreatetime(new Date()).setUpdatetime(new Date());
			page.save();
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("添加成功");
		}else{
			SinglePage page=new SinglePage();
			page.setTitle(getPara("title")).setKeywords(getPara("keywords"));
			page.setDescription(getPara("description")).setContent(getPara("content"));
			page.setUpdatetime(new Date());
			page.setId(id);
			page.update();
			resp.setCode(ApiConstant.CODE_SUCCESS);
			resp.setMsg("编辑成功");
		}
		renderJson(resp);
	}
	@RequiresPermissions(value={"/admin/page"})
	public void del(){
		String idStrs=this.getPara("ids");
		List<Integer> ids=CommonUtils.getIntegerListByStrs(idStrs);
		for(Integer id:ids){
			SinglePage.dao.deleteById(id);
		}
		ApiResp resp=new ApiResp();
		resp.setCode(ApiConstant.CODE_SUCCESS);
		resp.setMsg("删除成功");
		renderJson(resp);
	}
	@RequiresPermissions(value={"/admin/page"})
	public void setVisible(){
		Integer visible=this.getParaToInt("visible");
		Boolean display= visible==1?true:false;
		String idStrs=this.getPara("ids");
		List<Integer> ids=CommonUtils.getIntegerListByStrs(idStrs);
		List<SinglePage> modelList=new ArrayList<SinglePage>();
		for(int id:ids){
			SinglePage page=new SinglePage();
			page.setId(id);
			page.setDisplay(display);
			modelList.add(page);
		}
		Db.batchUpdate(modelList, modelList.size());
		this.renderJson(InvokeResult.success("设置成功"));
	}
}
